package com.example.myandroidapp1;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.myandroidapp1.activity.AbstractBaseActivity;
import com.example.myandroidapp1.sql.service.SqlDbService;

public class SqlLiteExample extends AbstractBaseActivity implements
		OnClickListener {

	private Button sqlUpdate;
	private Button sqlView;
	private EditText sqlName;
	private EditText sqlHotness;

	private EditText sqlRowId;
	private Button sqlViewInfo;
	private Button sqlEditEntry;
	private Button sqlDeleteEntry;

	@Override
	public void onClick(View v) {
		SqlDbService sqlDbService = new SqlDbService(this);
		
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bSqlUpdate:
			boolean didItWork = true;
			try {

				String name = sqlName.getText().toString();
				String hotness = sqlHotness.getText().toString();
				sqlDbService = new SqlDbService(this);
				sqlDbService.open();

				sqlDbService.createEntry(name, hotness);

				sqlDbService.close();
			} catch (Exception e) {
				// TODO: handle exception
				didItWork = false;
				Dialog d = new Dialog(this);
				d.setTitle("Heck Yea!!!");
				TextView tv = new TextView(this);
				tv.setText(e.getMessage());
				d.setContentView(tv);
				d.show();
			} finally {
				if (didItWork) {
					Dialog d = new Dialog(this);
					d.setTitle("Heck Yea!!!");
					TextView tv = new TextView(this);
					tv.setText("Success!!!");
					d.setContentView(tv);
					d.show();
				}
			}
			break;

		case R.id.bSqlView:
			Intent i = new Intent("com.example.myandroidapp1.SqlView");
			startActivity(i);
			break;

		case R.id.bSqlViewInfo:
			String s = sqlRowId.getText().toString();
			long l = Long.parseLong(s);
			sqlDbService.open();

			String returnedName = sqlDbService.getName(l);
			String returnedHotness = sqlDbService.getHotness(l);

			sqlDbService.close();

			sqlName.setText(returnedName);
			sqlHotness.setText(returnedHotness);
			break;
		case R.id.bSqlEditEntry:
			long row = Long.parseLong(sqlRowId.getText().toString());
			String hotness = sqlHotness.getText().toString();
			String name = sqlName.getText().toString();

			sqlDbService = new SqlDbService(this);
			sqlDbService.open();
			sqlDbService.updateEntry(row,name,hotness);
			sqlDbService.close();
			
			break;
		case R.id.bSqlDeleteEntry:
			long rowId = Long.parseLong(sqlRowId.getText().toString());
			
			sqlDbService = new SqlDbService(this);
			sqlDbService.open();
			sqlDbService.deleteEntry(rowId);
			sqlDbService.close();
			
			
			break;
		default:
			break;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sqlliteexample);
		sqlUpdate = (Button) findViewById(R.id.bSqlUpdate);
		sqlView = (Button) findViewById(R.id.bSqlView);
		sqlName = (EditText) findViewById(R.id.etSqlName);
		sqlHotness = (EditText) findViewById(R.id.etSqlHotness);

		sqlRowId = (EditText) findViewById(R.id.etSqlRowId);
		sqlViewInfo = (Button) findViewById(R.id.bSqlViewInfo);
		sqlEditEntry = (Button) findViewById(R.id.bSqlEditEntry);
		sqlDeleteEntry = (Button) findViewById(R.id.bSqlDeleteEntry);

		sqlViewInfo.setOnClickListener(this);
		sqlEditEntry.setOnClickListener(this);
		sqlDeleteEntry.setOnClickListener(this);
		sqlUpdate.setOnClickListener(this);
		sqlView.setOnClickListener(this);
	}
}
